<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Question extends Controller{
    function Question(){
        parent::Controller();
    }
    
    private function getQuestion($number){
		$this->load->model('question_m', 'qs', TRUE);
        $user = $this->input->post('username');
		if(isset($_POST['username']))
			echo 'username='. $_POST['username'];
		//$this->_Possession->userdata('username');
        //$res = $this->qs->getQuestion($user, $number);
        
        // echo 'question='. urlencode($res['question']). "&";
		// echo 'username='. $user ."&";
        // echo 'ch1='. $res['ch1']. "&";
        // echo 'ch2='. $res['ch2']. "&";
        // echo 'ch3='. $res['ch3']. "&";
        // echo 'ch4='. $res['ch4']. "&";
        // echo 'ch5='. $res['ch5'];
    }
	
	public function handle()
	{
		if (isset($_POST['systemCall']))
		{
			if ($_POST['systemCall'] == 'doLogin' )
			{
				if (isset($_POST['username']) && isset($_POST['password']))
				{
					
					if($this->do_login($_POST['username'], $_POST['password']))
					{
						$this->load->model('question_m', 'qs', TRUE);
						$out = $this->qs->getNumQuestion();
						echo 'dummy=0&';
						echo 'systemResult=true';
						echo '&numQuestion='. 25;
					}
					else
					{
						echo 'systemResult=false';
					}
				}
				else
				{
					echo 'systemResult=false';
				}
			}
			else if (($_POST['systemCall'] == 'getQuestion' ))
			{
				if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['num']))
				{
					$this->load->model('question_m', 'qs', TRUE);
					$res = $this->qs->getQuestion($_POST['username'], $_POST['password'], $_POST['num']);
					echo 'dummy=0&';
					if ($res!=0)
					{
						echo 'systemResult=true&';
						echo 'question='. urlencode($res['question']). "&";
						echo 'ch1='. urlencode($res['ch1']). "&";
						echo 'ch2='. urlencode($res['ch2']). "&";
						echo 'ch3='. urlencode($res['ch3']). "&";
						echo 'ch4='. urlencode($res['ch4']). "&";
						echo 'ch5='. urlencode($res['ch5']);
					}
					else
					{
						echo 'systemResult=false';
					}
				}
				else
				{
					echo 'systemResult=false';
				}
			}
			else if ($_POST['systemCall'] == 'postAnswer')
			{
				if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['num']) && isset($_POST['choice']))
				{
					if($this->do_login($_POST['username'], $_POST['password']))
					{
						$this->load->model('answer_m', 'qs', TRUE);
						$num = $_POST['num'];
						$res = $this->qs->setAnswer($_POST['username'], $_POST['password'], $_POST['num'], $_POST['choice']);
						echo 'dummy=0&';
						if ($res > 0)
							echo 'systemResult=4';
						else
							echo 'systemResult=3';
					}
					else
					{
						echo 'systemResult=2';
					}
				}
				else
				{
					echo 'systemResult=1';
				}
			}
		}
	}
	
	private function do_login($username, $password) {
        $this->db->where('username', $username);
        $this->db->where('password', $password);
        //$this->db->where('is_active', 1);
        $query = $this->db->get('users');
        if ($query->num_rows() == 1) {
            return true;
        } else {
            return false;
        }
    }
}
